// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex' 
import App from './App'
import router from './router'
import axios from 'axios'
import {Indicator } from 'mint-ui'
import 'mint-ui/lib/style.css'

const imgutil = require('./common/until.js')

Vue.use(Vuex)

let baseurl = ''
Vue.prototype.$http = function (url, data, doComplete, methos) {
  axios.defaults.headers.common['Authorization'] = localStorage.getItem('token') || ''
  const that = this
  if (url.substring(0, 5) === '/user') {
    baseurl = '/v4'
  } else {
    baseurl = '/v3'
  }
  const realurl = baseurl + url
  if(methos == 'GET'){
    axios.get(realurl,{params:data}).then(function (res) {
      if(res.data){
        if(res.data instanceof String){
          res.data = JSON.parse(res.data)
        }
        if(res.data.Code == '401'){
          that.$router.push({path:'/login/login'})
          window.location.href = '#/login/login'
        }
        if (typeof doComplete === "function") {
          doComplete(res.data)
        }
      }
    })
  }else if(methos == 'POST'){
    axios.post( realurl,data).then(function (res) {
      if(res.data){
        if (typeof doComplete === "function") {
          doComplete(res.data)
        }
      }
    })
  }
}



var store = new Vuex.Store({
  state: {
    SortMenus: [],
    Grid: [],
    sort: 0,
    filterValues: {},
    carList: {},
  },
  mutations: {
    changeMenus(state,arr){
      state.SortMenus = arr
    },
    changeGrid(state,arr){
      state.Grid = arr
    },
    changeSort(state,num){
      state.sort = num
    },
    changeFilterValues(state,obj){
      state.filterValues = obj
    },
    changeCarList(state,carList){
      state.carList = carList
    }
  },
  getters: {
    getSortMenus(state){
      return state.SortMenus
    },
    getGrid(state){
      return state.Grid
    },
    getSort(state){
      return state.sort
    },
    getFilterValues(state){
      return state.filterValues
    },
    getCarList(state){
      return state.carList
    }
  },
  actions:{
    addCarList({commit}){
      Indicator.open()
      Vue.prototype.$http('/pinhuocart/GetItems3',{},function(res){
        Indicator.close()
        if(res.Result){
          res.Data.Items.map(item1 => {
            item1.selected = true
            item1.count = 0
            item1.TimeList.map(item2 => {
              item2.Items.map(item3 => {
                item3.selected = true
                item3.Cover = imgutil.getUrl(item3.Cover,400)
                item1.count++
                item3.colorList = []
                let colorArr = []
                item3.Products.map(item4 => {
                  item4.msg = item4.Size + "/" + item4.Qty + "件";
                  colorArr.push(item4.Color)
                })
                colorArr = Array.from(new Set(colorArr))
                colorArr.map(m => {
                    var tempObj = { color: m, SizeList: [] };
                    item3.Products.map(item5 => {
                        if (item5.Color == m) {
                            tempObj.SizeList.push(item5.msg)
                        }
                    })
                    tempObj.msg = tempObj.SizeList.join('，');
                    item3.colorList.push(tempObj);
                })
              })
            })
          })
          commit('changeCarList',res.Data)
        }
      },'GET')
    }
  }
})



Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
